package com.itheima.hmshopping.app.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.hmshopping.dubbo.api.PmsBrandApi;
import com.itheima.hmshopping.entity.PmsBrand;
import com.itheima.hmshopping.exceptionhander.CustomException;
import com.itheima.hmshopping.util.HmPage;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class PmsBrandService {

    @DubboReference
    private PmsBrandApi pmsBrandApi;

    /**
     * 获取全部品牌列表
     *
     * @return
     */
    public List<PmsBrand> getListAll() {
        //1.调用远程服务通过id查询全部品牌数据
        List<PmsBrand> list = pmsBrandApi.getListAll();
        return list;
    }

    /**
     * 根据品牌名称分页获取品牌列表
     *
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    public HmPage getList(Integer pageNum, Integer pageSize, String keyword) {
        Page page = pmsBrandApi.findByBrand(pageNum, pageSize, keyword);
        HmPage hmPage = new HmPage();
        //1->设置总页数
        hmPage.setTotalPage(Convert.toInt(page.getPages()));
        //2->查询数据
        hmPage.setList(page.getRecords());
        //3->查询第几页
        hmPage.setPageNum(Convert.toInt(pageNum));
        //4->查询出总页数
        hmPage.setTotalPage(Convert.toInt(page.getTotal()));
        //5->查询每页的条数
        hmPage.setPageSize(Convert.toInt(pageSize));
        return hmPage;
    }


    /**
     * 根据编号查询品牌信息
     *
     * @param id
     * @return
     */
    public PmsBrand getById(Integer id) {
        PmsBrand pmsBrand = pmsBrandApi.findById(id);
        return pmsBrand;
    }

    /**
     * 添加品牌
     *
     * @param pmsBrand
     */
    public void save(PmsBrand pmsBrand) {
        //1->将数据进行封装
        PmsBrand brand = new PmsBrand();
        brand.setLogo(pmsBrand.getLogo());
        brand.setName(pmsBrand.getName());
        brand.setBigPic(pmsBrand.getBigPic());
        brand.setBrandStory(pmsBrand.getBrandStory());
        brand.setFactoryStatus(pmsBrand.getFactoryStatus());
        brand.setFirstLetter(pmsBrand.getFirstLetter());
        brand.setShowStatus(pmsBrand.getShowStatus());
        brand.setSort(pmsBrand.getSort());
        pmsBrandApi.save(brand);
    }

    /**
     * 更新品牌
     * @param id
     * @param brand
     */
    public void updateById(Integer id, PmsBrand brand) {
        PmsBrand pmsBrand =new PmsBrand();
        pmsBrand.setLogo(brand.getLogo());
        pmsBrand.setName(brand.getName());
        pmsBrand.setBigPic(brand.getBigPic());
        pmsBrand.setBrandStory(brand.getBrandStory());
        pmsBrand.setFactoryStatus(brand.getFactoryStatus());
        pmsBrand.setFirstLetter(brand.getFirstLetter());
        pmsBrand.setShowStatus(brand.getShowStatus());
        pmsBrand.setSort(brand.getSort());
        pmsBrandApi.updateById(id,pmsBrand);
    }

    /**
     * 删除品牌
     * @param id
     */
    public void deleteById(Integer id) {
        //1.先查询删除的数据是否存在/不存在则抛出异常
        PmsBrand pmsBrand = pmsBrandApi.findById(id);
        if (ObjectUtil.isEmpty(pmsBrand)){
            throw new CustomException("品牌不存在");
        }
        pmsBrandApi.deleteById(id);
    }

    /**
     * 批量删除品牌
     * @param ids
     */
    public void deleteByIds(Integer[] ids) {
        if (ids==null||ids.length==0){
            throw new CustomException("暂无数据");
        }
        pmsBrandApi.deleteByIds(ids);
    }


    /**
     * 批量更新显示状态
     * @param ids
     * @param showStatus
     */
    public void updateStatus(Integer[] ids, Integer showStatus) {
        if (ids==null||ids.length==0){
            throw new CustomException("暂无数据");
        }
        pmsBrandApi.updateStatus(ids,showStatus);
    }


    /**
     * 批量更新厂家制造商状态
     * @param ids
     * @param factoryStatus
     */
    public void updateFactoryStatus(Integer[] ids, Integer factoryStatus) {
        if (ids==null||ids.length==0){
            throw new CustomException("暂无数据");
        }
        pmsBrandApi.updateFactoryStatus(ids,factoryStatus);
    }
}